//
//  APIs.m
//  YaDiuDiu
//
//  Created by Zephyr on 2017/9/18.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "YDDAPIs.h"
#import "AFNetworking.h"
#import "YDDConstant.h"
#import "YDDUser.h"

NSString * const imageUploadAPI = @"http://ydd.xtangguo.com/index.php/App/Norm/upload";
NSString * const userCenterHomeDataAPI = @"http://ydd.xtangguo.com/index.php/App/Usercenter/index";

@implementation YDDAPIs

static YDDAPIs *_apis;

+ (instancetype)share {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _apis = [YDDAPIs new];
    });
    return _apis;
}

+ (void)getWithApi:(NSString *)api params:(NSDictionary *)params success:(void(^)(NSDictionary *result))success failure:(void(^)(NSError * error))failure {
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil];
    [manager GET:api parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
    
}

+ (void)postWithApi:(NSString *)api params:(NSDictionary *)params success:(void(^)(NSDictionary *result))success failure:(void(^)(NSError * error))failure {
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil];
    [manager POST:api parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
    
}

+ (void)uploadImage:(NSData *)imgData success:(void(^)(NSString *imagePath, NSString *errMsg))success failure:(void(^)(NSError * error))failure {
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil];
    
    [manager POST:imageUploadAPI parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        [formData appendPartWithFileData:imgData name:@"pic" fileName:@"pic.jpg" mimeType:@"image/jpeg"];
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success) {
            NSDictionary *dict = (NSDictionary *)responseObject;
            if ([dict.allKeys containsObject:resultIDKey] && [dict[resultIDKey] intValue] == 101 && [dict.allKeys containsObject:resultDataKey]) {
                NSString *imagePath = dict[resultDataKey];
                success(imagePath, nil);
            }
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
    
}

+ (void)userCenterDataSuccess:(void (^)(YDDUser *, NSString *))success failure:(void (^)(NSError *))failure {
    
    if (![YDDAPIs share].token.length) {
        YLog(@"Token为空，请先登录");
    }
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:[YDDAPIs share].userID forKey:userIDKey];
    [params setValue:[YDDAPIs share].token forKey:verifyTokenKey];
    
    [self postWithApi:userCenterHomeDataAPI params:params success:^(NSDictionary *result) {
        if (success) {
            if ([result.allKeys containsObject:resultIDKey] && [result[resultIDKey] intValue] == 200 && [result.allKeys containsObject:resultDataKey] && [result[resultDataKey] isKindOfClass:[NSDictionary class]]) {
                YDDUser *yUser = [YDDUser userWithDict:result[resultDataKey]];
                [YDDAPIs share].yUser = yUser;
                success(yUser, nil);
            } else if ([result.allKeys containsObject:resultMsgKey]) {
                success(nil, result[resultMsgKey]);
            }
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
    
}


@end
